<?php

namespace App\Http\Controllers\Admin;

use App\Repository\Shop\SuppilerRepository as model;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller as BaseController;

// 分销商
class DistributorsController extends BaseController
{
  public function __construct(model $model)
  {
    $this->repoist = $model;
  }
  public function index(Request $request)
  {
    $where = [];
    if (!empty($request->get('phone'))) {
      $where['where']['phone'] = $request->get('phone');
    }
    if (!empty($request->get('name'))) {
      $where['like'] = ['name' => $request->get('name')];
    }
    if (!empty($request->get('number'))) {
      $where['like'] = ['name' => $request->get('number')];
    }
    $where['limit']['start'] = ($request->get('pageNum') - 1) * $request->get('pageSize');
    $where['limit']['number'] = $request->get('pageSize');
    $userSupplier = $request->user->supplier;
    $where['where'][] = ['supplier', '=', $userSupplier];
    $where['select'] = ['account_manager', 'account_manager_id', 'dist_code', 'id', 'remark_name', 'name', 'phone', 'created_at', 'updated_at', 'supplier_name'];
    $data =  $this->repoist->index($where);
    $data['supplier'] = $request->user->supplier;
    return $data;
  }

  public function destroy($id)
  {
    if (!$id) {
      return $this->returnMsg(1, '请传递参数');
    }
    $where = [
      'id' => $id
    ];
    return $this->repoist->del($where);
  }
  public function save(Request $request)
  {
    if (!$request->get('id')) {
      return $this->returnMsg(1, '请传递id参数');
    }
    $where = ['id' => $request->get('id'), 'supplier_id' => $request->user->shop_id];
    $data = [
      $request->get('type') => $request->get('field'),
    ];
    // var_dump($data);
    return $this->repoist->edit($where, $data);
  }
}
